package com.sysu.simulation.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sysu.simulation.R;
import com.sysu.simulation.bean.Clothes;


public class SimClothesAdapter extends BaseRecyclerAdapter<Clothes> implements View.OnClickListener{
    private OnFooterClickListener mFooterListener;
    private int selectedUpper = -1;
    private int selectedUnder = -1;

    public SimClothesAdapter(Context context) {
        super(context,1);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type) {
        return new SimClothesViewHolder(mInflater.inflate(R.layout.item_sim_clothes, parent, false));
    }

    @Override
    protected void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, Clothes item, int position) {
        SimClothesViewHolder vh = (SimClothesViewHolder) holder;
        vh.mTitle.setText(item.getTitle());
        if(item.getType().ordinal()<3) vh.mType.setText("上衣");
        else vh.mType.setText("下衣");
        //if(position == selectedUpper || position == selectedUnder) vh.itemView.setSelected(true);
        //else vh.itemView.setSelected(false);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        SimClothesViewHolder holder = new SimClothesViewHolder(mInflater.inflate(R.layout.item_sim_clothes, parent, false));
        holder.itemView.setOnClickListener(this);
        return holder;
    }

    @Override
    protected void onBindFooterViewHolder(RecyclerView.ViewHolder holder) {
        SimClothesViewHolder vh = (SimClothesViewHolder) holder;
        vh.mTitle.setText("更多服装...");
        vh.mType.setText("");
    }

    @Override
    public void onClick(View view) {
        if(mFooterListener != null) {
            mFooterListener.onFooterClick();
        }
    }

    public void setOnFooterClickListener(OnFooterClickListener fl) {
        if(fl != null) {
            mFooterListener = fl;
        }
    }

    public void setSelectedItem(int position, int type) {
        int oldPosition = -1;
        if(type == 0) {
            oldPosition = selectedUpper;
            selectedUpper = position;
        }
        else if(type == 1) {
            oldPosition = selectedUnder;
            selectedUnder = position;
        }
        if(oldPosition>=0) notifyItemChanged(oldPosition);
        notifyItemChanged(position);
    }

    private class SimClothesViewHolder extends RecyclerView.ViewHolder {
        private TextView mTitle, mType;

        public SimClothesViewHolder(View itemView) {
            super(itemView);
            mTitle = (TextView) itemView.findViewById(R.id.sim_clothes_title);
            mType = (TextView) itemView.findViewById(R.id.sim_clothes_type);
        }
    }

    public interface OnFooterClickListener {
        void onFooterClick();
    }
}
